<?php

/**
 * Created by PhpStorm.
 * User: aoxiangli
 * Date: 2017/6/27 10:53
 * Description: 后台管理通用文件
 */
class BmCommon extends CI_Controller
{
    protected $pre_token = "bm_token_";
    protected $login_user_id = 0;

    protected function checkSession()
    {
        if (!isset($_SESSION['adminUser'])) {
            $this->tools->echoJson($this->errorcode->noLogin);
        }
        $this->login_user_id = $_SESSION['adminUser']['uid'];
    }

    //查询相对应的列表
    protected function commonList($where, $table, $page, $page_size, $left_join, $field = "*", $order_by = 'id DESC')
    {
        $sql = "SELECT count(*) AS c FROM {$table} ";
        if ($left_join) {
            $sql .= $left_join;
        }
        $sql .= " WHERE {$where}";
        $result = $this->db->query($sql);
        $count = $result->row_array();
        $count = $count['c'];
        $sql = "SELECT {$field} FROM {$table} ";
        if ($left_join) {
            $sql .= $left_join;
        }
        $page = ($page - 1) * $page_size;
        $sql .= " WHERE {$where} ORDER BY {$order_by} LIMIT {$page},{$page_size}";
        $result = $this->db->query($sql);
        $data = $result->result_array();
        return array($count, $data);
    }
}